// pages/learn/onlinelearn.js
import $ from '../common';

Page({
  data: {
    isshow:false,
    currentIndex: 0,
    tipone:'上滑加载更多',
    tiptwo:'上滑加载更多',
    tipthree:'上滑加载更多',
    pageone:1,
    pagetwo:1,
    pagethree:1,
    arrone:[],
    arrtwo:[],
    arrthree:[],
    category:'',
    vcourse_name:''
  },
  onLoad() {
    this.setData({
      zonecode:wx.getStorageSync('addresscode')||''
    })
    this.gethot(),
    this.getselect()
    this.getall()
  },
  onShow() {},
  // 加载更多
  gomore(){},
  // 热门
  gethot(){
    let {arrone}=this.data
    $.get('/vcourse/hot',{
      zonecode:this.data.zonecode,
      limit:20,
      page:this.data.pageone
    },res=>{
      console.log('热门');

      this.data.pageone++
      if(res.vcourses.length<20){
        this.data.tipone='已经到底了'
      }else{
        this.data.tipone='上滑加载更多'
      }
      this.setData({
        pageone:this.data.pageone,
        tipone:this.data.tipone,
        arrone:[...arrone,...res.vcourses]
      })
    })
  },
  // 精选
  getselect(){
    let {arrtwo}=this.data
    $.get('/vcourse/featured',{
      zonecode:this.data.zonecode,
      limit:20,
      page:this.data.pagetwo
    },res=>{
      console.log('精选');
      // console.log(res);
      this.data.pagetwo++
      if(res.vcourses.length<20){
        this.data.tiptwo='已经到底了'
      }else{
        this.data.tiptwo='上滑加载更多'
      }
      this.setData({
        pagetwo:this.data.pagetwo,
        tiptwo:this.data.tiptwo,
        arrtwo:[...arrtwo,...res.vcourses]
      })
    })
  },
  // 全部课程
  getall(){
    let {arrthree}=this.data
    $.get('/vcourse/query',{
      zonecode:this.data.zonecode,
      category:this.data.category,
      limit:20,
      page:this.data.pagethree,
      vcourse_name:this.data.vcourse_name
    },res=>{
      console.log('全部课程');
      this.data.pagethree++
      if(res.vcourses.length<20){
        this.data.tipthree='已经到底了'
      }else{
        this.data.tipthree='上滑加载更多'
      }
      this.setData({
        pagethree:this.data.pagethree,
        tipthree:this.data.tipthree,
        arrthree:[...arrthree,...res.vcourses]
      })
    })
  },
  onChange(e) { //输入框
    this.setData({
      vcourse_name: e.detail,
    });
  },
  onSearch() { //确定按钮
    this.init()
  },
  onClick() { //点击按钮
    this.init()
  },
  clear(){//清除
    this.setData({
      vcourse_name:''
    })
    this.init()
  },  
  // 重置
  init(){
    this.setData({
      pagethree:1,
      tipthree:'上拉加载更多',
      arrthree:[]
    })
    this.getall()
  },
  // pagechange: function (e) {//滑动
  //   let current = e.detail.current;
  //   this.setData({
  //     currentIndex: current
  //   })
  // },
  titleClick(e) {//切换点击
    const {currentIndex}=this.data
    if(currentIndex==e.currentTarget.dataset.idx) return
    this.setData({
      //拿到当前索引并动态改变
      currentIndex: e.currentTarget.dataset.idx
    })
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },
  // 打开筛选
  openorclose(){
    const{isshow}=this.data
    this.setData({
      isshow:!isshow
    })
  },
  onReachBottom(){
    const {currentIndex}=this.data
    console.log('触底了！！！');
    if(currentIndex==0){
      this.gethot()
    }else if(currentIndex==1){
      this.getselect()
    }else{
      this.getall()
    }
  }
})